import { FMVCS } from "../../fgui/fmvc/FMVCS";
import EventSystem from "../../core/EventSystem";
import { GlobalEvent } from "../global/Global";
import MapLocalCtrl from "./MapLocalCtrl";
import MapItem from "../component/mapitem/MapItem";
import PlayerCtrl from "../player/PlayerCtrl";
import { ConstDefine } from "../common/ConstDefine";

export default class MapLocalView extends FMVCS.FView {
    /** 地图列表 */
    mapList: fgui.GList = null;

    clickHandlers(){
        return [
            [this._views[`btnMapCreate`], this.onBtnMapCreate, this],
            [this._views[`btnBack`], this.onBtnBackHome, this],
        ];
    }

    onOpenCallBack(){
        fgui.UIObjectFactory.setExtension("ui://MapCreate/btnMap", MapItem);
        this.mapList = this._views[`mapList`].asList;
        this.rigisterEventListener();
        this.initMapList();
        this.onFlush();
    }

    rigisterEventListener(){
        EventSystem.on(GlobalEvent.UIListenID.UI_GameMapFlush, this.onMapFlush, this);
    }

    initMapList(){
        this.mapList.setVirtual();
        this.mapList.itemRenderer = this.onItemRender.bind(this);
        this.mapList.numItems = MapLocalCtrl.data.vo.mapList.length;
    }

    onItemRender(index: number, obj: MapItem){
        let map = MapLocalCtrl.data.vo.mapList[index];
        obj.onClick(this.onItemClick, this);
        obj.index = index;
        obj.mapName = map.mapName;
        obj.userName = PlayerCtrl.data.vo.nickName;
        obj.onFlush();
    }

    onItemClick(e: fgui.Event){
        let target = e.currentTarget.$gobj as MapItem;
        let mapIndex = target.index;
        MapLocalCtrl.data.vo.playMapIndex = mapIndex;
        let map = MapLocalCtrl.data.vo.mapList[mapIndex];
        cc.log(`地图索引：${mapIndex} 地图id: ${map.id}`);
        let playCustomMap = () => {
            MapLocalCtrl.onPlayCustomMap(mapIndex);
            this.onClose();
        }

        let tipsData: ConstDefine.Tips.ButtonTipsEventData = {
            buttonNum: 3,
            text: `房间名:\n` + MapLocalCtrl.data.vo.mapList[mapIndex].mapName,
            btn1Text: `挑战`,
            btn2Text: `删除`,
            btn3Text: `修改`,
            btn1CallBack: playCustomMap,
            btn2CallBack: () => {
                MapLocalCtrl.data.removeMap(mapIndex);
                this.initMapList();
            },
            btn3CallBack: () => {
                EventSystem.emit(GlobalEvent.UIListenID.UI_MapRevisions, map);
                this.onClose();
            }
        }
        EventSystem.emit(GlobalEvent.UIListenID.UI_ButtonTipsView_Open, tipsData);
    }

    onBtnMapCreate(){
        EventSystem.emit(GlobalEvent.UIListenID.UI_BtnMapCreate_Click);
        this.onClose();
    }

    onBtnBackHome(){
        EventSystem.emit(GlobalEvent.UIListenID.UI_HomeView_Open);
        this.onClose();
    }

    onMapFlush(){
        this.initMapList();
        this.onFlush();
    }

    onFlush(type: string = `all`){
        switch(type){
            case `all`: {
                this._updateLblText();
                break;
            }
        }
    }

    private _updateLblText(){
        this._views[`lblTips`].asLabel.visible = this.mapList._children.length <= 0;
    }
}